package service

import (
	"anban/models"
	"anban/utils"
	"github.com/astaxie/beego/orm"
	"net/url"
	"strconv"
)

// 获取交易记录信息
func GetFinanceInfo(p map[string]interface{}) *models.Finance {
	o := orm.NewOrm()
	finance := &models.Finance{}
	qs := o.QueryTable("Finance")
	if _, ok := p["relation"]; ok && p["relation"].(bool) {
		qs = qs.RelatedSel()
	}
	if _, ok := p["id"]; ok {
		qs = qs.Filter("id", p["id"])
	}
	qs.One(finance)
	if _, ok := p["convert"]; ok && p["convert"].(bool) {
		if finance.Id > 0 {
			finance.TypeShow = models.FinanceType[finance.Type]
			finance.CreatedShow = finance.Created.Format("2006-01-02 15:04:05")
		}
	}
	return finance
}

// 添加交易记录信息
func AddFinance(input url.Values) (int64, error) {
	o := orm.NewOrm()
	finance := &models.Finance{}
	user := &models.User{}
	if _, ok := input["user_id"]; ok {
		user.Id = utils.Atoi64(input["user_id"][0])
		o.Read(user)
	}
	finance.User = user
	student := &models.Student{}
	if _, ok := input["student_id"]; ok {
		student.Id = utils.Atoi64(input["student_id"][0])
		o.Read(student)
	}
	finance.Student = student
	if _, ok := input["type"]; ok {
		finance.Type, _ = strconv.Atoi(input["type"][0])
	}
	if _, ok := input["amount"]; ok {
		finance.Amount = utils.Atof64(input["amount"][0])
	}
	return o.Insert(finance)
}

// 修改交易记录信息
func EditFinance(input url.Values) (int64, error) {
	o := orm.NewOrm()
	p := orm.Params{}
	for k, v := range input {
		if k == "id" || k == "_xsrf" {
			continue
		}
		p[k] = v[0]
	}
	return o.QueryTable("Finance").Filter("id", input["id"][0]).Update(p)
}

// 获取交易记录列表
func GetFinanceList(p map[string]interface{}) (int64, []*models.Finance) {
	var financeList []*models.Finance
	o := orm.NewOrm()
	qs := o.QueryTable("Finance")
	if _, ok := p["relation"]; ok && p["relation"].(bool) {
		qs = qs.RelatedSel()
	}
	if _, ok := p["type"]; ok {
		qs = qs.Filter("type", p["type"])
	}
	if _, ok := p["realName"]; ok {
		qs = qs.Filter("User__RealName", p["realName"])
	}
	if _, ok := p["phone"]; ok {
		qs = qs.Filter("User__Phone", p["phone"])
	}
	if _, ok := p["startTime"]; ok {
		qs = qs.Filter("created__gt", p["startTime"])
	}
	if _, ok := p["endTime"]; ok {
		qs = qs.Filter("created__lt", p["endTime"])
	}
	totalCount, _ := qs.Count()
	curPage, ok := p["curPage"]
	perCount, _ := p["perCount"]
	if ok {
		start := perCount.(int) * (curPage.(int) - 1)
		qs = qs.Limit(perCount, start)
	}
	qs.OrderBy("-id").All(&financeList)
	if _, ok := p["convert"]; ok && p["convert"].(bool) {
		for _, finance := range financeList {
			finance.TypeShow = models.FinanceType[finance.Type]
			finance.CreatedShow = finance.Created.Format("2006-01-02 15:04:05")
		}
	}
	return totalCount, financeList
}

// 获取交易记录总金额
func GetFinanceTotalAcmount(p map[string]interface{}) float64 {
	var financeList []*models.Finance
	o := orm.NewOrm()
	qs := o.QueryTable("Finance")
	if _, ok := p["type"]; ok {
		qs = qs.Filter("type", p["type"])
	}
	if _, ok := p["realName"]; ok {
		qs = qs.Filter("User__RealName", p["realName"])
	}
	if _, ok := p["phone"]; ok {
		qs = qs.Filter("User__Phone", p["phone"])
	}
	if _, ok := p["startTime"]; ok {
		qs = qs.Filter("created__gt", p["startTime"])
	}
	if _, ok := p["endTime"]; ok {
		qs = qs.Filter("created__lt", p["endTime"])
	}
	qs.All(&financeList)
	var totalAmount float64
	for _, finance := range financeList {
		totalAmount += finance.Amount
	}
	return totalAmount
}